package cn.kgc.controller;

import cn.hutool.core.util.StrUtil;
import cn.kgc.annotation.LoginType;
import cn.kgc.annotation.RequireLogin;
import cn.kgc.entity.Resume;
import cn.kgc.entity.ResumeProjectExp;
import cn.kgc.entity.ResumeSearchForm;
import cn.kgc.form.ResumeUpdateForm;
import cn.kgc.form.ResumeWorkExpUpdateForm;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.interceptor.JwtInterceptor;
import cn.kgc.service.ResumeProjectExpService;
import cn.kgc.service.ResumeTccService;
import cn.kgc.service.ResumeWorkExpService;
import cn.kgc.service.impl.ResumeServiceImpl;
import cn.kgc.utils.PagedGridResult;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/resume")
@Api(tags = "简历模块")
@Validated
@Slf4j
@SuppressWarnings("all")
public class ResumeController {
    @Resource
    private ResumeTccService resumeTccService;

    @Resource
    private ResumeServiceImpl resumeService;


    @ApiOperation(value = "HR查询用户投递的简历信息")
    @PostMapping("/hrQueryResume")
    @RequireLogin({
            LoginType.USER_APP_LOGIN
    })
    public  GraceJSONResult hrQueryResume(String resumeId){
        return GraceJSONResult.ok(resumeService.queryResumeVo(resumeId));
    }

    @PostMapping("/pageResume")
    @ApiOperation(value = "分页查询简历信息")
//    @RequireLogin({
//            LoginType.USER_SAAS_LOGIN
//    })
    public PagedGridResult pageResume(@RequestBody ResumeSearchForm form){
        return resumeService.pageResume(form);
    }

    @PostMapping("/refreshResume")
    @ApiOperation(value = "将简历信息刷新到ES中")
    public GraceJSONResult refreshResume(String resumeId, String userId) {
        resumeService.refreshResume(resumeId, userId);
        return GraceJSONResult.ok();
    }


    @PostMapping("/app/init")
    @ApiOperation(value = "初始化用户简历")
    public GraceJSONResult initResume(@RequestParam("userId") String userId) {
        return GraceJSONResult.ok(
                resumeTccService.tryInitResume(null, userId)
        );
    }

    @PostMapping("/app/modifyResume")
    @ApiOperation(value = "修改简历基本信息")
    public GraceJSONResult modifyResume(@RequestBody ResumeUpdateForm form) {
        //获得当前登录的用户ID
        String userId = JwtInterceptor.userIds.get();
        if (StrUtil.isNotBlank(userId)) {
            //求职者修改的简历是否与当前登录的求职的
            Resume resume = resumeService.getById(form.getId());
            //如果登录的用户ID和简历的用户ID不一致，则没有权限修改
            if (!userId.equals(resume.getUserId())) {
                return GraceJSONResult.errorMsg("您没有权限修改简历...");
            }
            //更新简历信息
            return GraceJSONResult.ok(resumeService.updateResume(form));
        }
        return GraceJSONResult.errorMsg("您没有权限修改简历...");
    }

    @PostMapping("/app/queryResume")
    @ApiOperation("预览自己的简历信息")
    @SentinelResource(value = "queryResume")//资源的名字
    public GraceJSONResult queryResume(String id) {
        String userId = JwtInterceptor.userIds.get();
        Resume resume = resumeService.getById(id);
        if (userId.equals(resume.getUserId())) {
            return GraceJSONResult.ok(resume);
        }
        return GraceJSONResult.errorMsg("您没有权限查看简历...");
    }

    @Resource
    private ResumeWorkExpService resumeWorkExpService;

    @PostMapping("/app/queryResumeWorkExp")
    @ApiOperation("预览自己的工作经历")
    @RequireLogin(
            value = {LoginType.USER_APP_LOGIN}
    )
    public GraceJSONResult queryResumeWorkExp(String id) {
        //获得登录的用户id
        String userId = JwtInterceptor.userIds.get();
        //根据简历id和用户id查询工作经历
        return GraceJSONResult.ok(resumeWorkExpService.queryByResumeId(id, userId));
    }

    @PostMapping("/app/modifyResumeWorkExp")
    @ApiOperation("新增/修改工作经历")
    public GraceJSONResult modifyResumeWorkExp(@RequestBody ResumeWorkExpUpdateForm form) {
        String userId = JwtInterceptor.userIds.get();
        if (StrUtil.isNotBlank(userId)) {
            //新增/修改简历
            return GraceJSONResult.ok(resumeWorkExpService.addOrUpdate(form, userId));
        }
        return GraceJSONResult.errorMsg("您没有权限新增/修改简历...");
    }

    @PostMapping("/app/deleteResumeWorkExp")
    @ApiOperation("删除工经历")
    @RequireLogin(
            value = {LoginType.USER_APP_LOGIN}
    )
    public GraceJSONResult deleteResumeWorkExp(
            @RequestParam("workExpId") String workExpId,
            @RequestParam("resumeId") String resumeId
    ) {
        String userId = JwtInterceptor.userIds.get();
        return GraceJSONResult.ok(resumeWorkExpService.delete(workExpId, userId, resumeId));
    }

    @Resource
    private ResumeProjectExpService resumeProjectExpService;

    @PostMapping("/app/queryResumeProjectExp")
    @ApiOperation("预览自己的项目经验")
    @RequireLogin(
            value = {LoginType.USER_APP_LOGIN}
    )
    public GraceJSONResult queryResumeProjectExp(
            String id,
            String resumeId
    ) {
        String userId = JwtInterceptor.userIds.get();
        LambdaQueryWrapper<ResumeProjectExp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ResumeProjectExp::getResumeId, resumeId);
        wrapper.eq(ResumeProjectExp::getId, id);
        wrapper.eq(ResumeProjectExp::getUserId, userId);
        return GraceJSONResult.ok(resumeProjectExpService.getOne(wrapper));
    }
    
}